package com.benio.ycfdisplayer;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.benio.ycfdisplayer.displayer.DisplayListener;
import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.YcfDisplayer;
import com.benio.ycfdisplayer.fresco.photoview.FrescoPhotoView;

import java.util.LinkedList;
import java.util.Queue;

public class PhotoPagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * FrescoPhotoView+ViewPager使用时，复用View时需要注意:
         * 如果我们先将FrescoPhotoView add到ViewGroup中，再请求加载图片到FrescoPhotoView。
         * 因为复用FrescoPhotoView，View中的Controller还会带有上次的请求信息。
         * 所以当该view执行onAttach时，会先执行上一个请求，再执行本次请求。
         * 如果设置了Listener，会出现回调两次的后果。
         *
         * 解决方法：
         * 1.在复用FrescoPhotoView前，View中的Controller的请求信息清除。
         * 可在add到ViewGroup前或回收View的时候进行该操作。
         * 2.先请求加载图片到FrescoPhotoView，再add到ViewGroup。
         * 这样就能保证View中的请求信息永远是最新的。只需修改代码顺序，不需要做另外的操作。
         */
        setContentView(R.layout.activity_photo_pager);
        YcfDisplayer.initialize(this);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        PagerAdapter pagerAdapter = new MyPagerAdapter(DataUtil.IMAGES);
        viewPager.setAdapter(pagerAdapter);
    }

    static class MyPagerAdapter extends PagerAdapter {
        private Queue<View> mQueue = new LinkedList<>();
        private String[] mData;
        private Options mOptions;

        public MyPagerAdapter(String[] data) {
            mData = data;
            mOptions = new Options.Builder()
                    .setPlaceHolderImage(new ColorDrawable(Color.GREEN))
                    .setFailureImage(new ColorDrawable(Color.RED))
                    .build();
        }

        @Override
        public int getCount() {
            return mData.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mQueue.poll();
            if (view == null) {
                view = new FrescoPhotoView(container.getContext());
            }
            // 方法一：
            //container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            //YcfDisplayer.get().display(mData[position]).options(mOptions).listener(mListener).into(view);

            // 方法二：
            YcfDisplayer.get().display(mData[position]).options(mOptions).listener(mListener).into(view);
            container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return view;
        }

        private static final String TAG = "FrescoPager";

        private DisplayListener mListener = new DisplayListener() {

            @Override
            public void onSuccess(String imageUri, View view, Object loadedData) {
                Log.d(TAG, "onSuccess() [" + imageUri + "], view = [" + view + "], loadedData = [" + loadedData + "]");
            }

            @Override
            public void onFailure(String imageUri, View view, Throwable throwable) {
                Log.d(TAG, "onFailure() [" + imageUri + "], view = [" + view + "], throwable = [" + throwable + "]");
            }
        };

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
            // 方法一：
            // 因为复用了view，所以要在销毁view的时候将当前请求销毁
            // 若不销毁请求，会执行两次加载。一次为上次的请求，一次为当前请求
            // YcfDisplayer.get().display(null).into(view);
            mQueue.offer(view);
        }
    }
}
